<?php

namespace App\Console\Commands\Clickhouse;

use App\Models\Common\ConfigModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class MergeTableRepeatRowCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'clickhouse:mergeTableRepeatRow {connect} {table}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '合并表重复行';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * @throws \Exception
     */
    public function handle()
    {
        $connect = $this->input->getArgument("connect");
        $table   = $this->input->getArgument("table");

        try {
            // 执行SQL
            execTime(function () use ($connect, $table) {
                DB::connection($connect)->statement("OPTIMIZE table {$table} final;");
            }, $table);

            // 添加日志
            Log::info("ClickHouse优化表格索引-连接配置：{$connect} 数据表：{$table}");

            return parent::SUCCESS;
        } catch (\Exception $exception) {
            logException(
                "ClickHouse优化表格索引-连接配置：{$connect} 数据表：{$table} 异常",
                $exception,
                [
                    "connect" => $connect,
                    "table"   => $table
                ],
                true,
                true
            );
            return parent::FAILURE;
        }
    }
}
